﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalRune.Windows.TextEditor;
using Zove.Runtime.Queries.Editor;
using System.IO;
using System.Configuration;

namespace Zove.Modeling.Editor {
    public partial class Form1 : Form {

        public string FileName {
            get;
            set;
        }

        public TextArea TextArea {
            get {
                return this.tbSource.ActiveTextAreaControl.TextArea;
            }
        }

        public Form1() {
            InitializeComponent();

            this.Load += new EventHandler(Form1_Load);
        }

        public ImageList imageList1;

        public void Form1_Load(object sender, EventArgs e) {
            this.tbSource.Font = new Font(ConfigurationManager.AppSettings["font"],
                Convert.ToInt32(ConfigurationManager.AppSettings["font size"]));
            CodeCompletionKeyHandler.Attach(this, this.tbSource);
        }

        private void cmdRun_Click(object sender, EventArgs e) {
            Run();
        }

        private void runToolStripMenuItem1_Click(object sender, EventArgs e) {
            Run();
        }

        public void Save() {
            if (string.IsNullOrEmpty(this.FileName)) {
                SaveAs();
            } else {
                this.tbSource.SaveFile(this.FileName);
            }
        }

        public void SaveAs() {
            SaveFileDialog d = new SaveFileDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                this.FileName = d.FileName;
                this.tbSource.SaveFile(d.FileName);
            }
        }

        public void Run() {
            Save();
            if (string.IsNullOrEmpty(FileName)) {
                return;
            }

            QueryEditor editor = new QueryEditor();

            FileInfo f = new FileInfo(this.FileName);
            editor.Source = "select * from " + f.Name.Split('.')[0] + ";";

            editor.Show();
            editor.Run();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
            Close();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e) {
            SaveFile(sender, e);
        }

        private void SaveFile(object sender, EventArgs e) {
            if (string.IsNullOrEmpty(FileName)) {
                SaveAs(sender, e);
            } else {
                tbSource.SaveFile(FileName);
            }
        }

        private void SaveAs(object sender, EventArgs e) {
            SaveFileDialog d = new SaveFileDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                this.FileName = d.FileName;
                tbSource.SaveFile(d.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e) {
            OpenFile();
        }

        private void OpenFile() {
            OpenFileDialog d = new OpenFileDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                UpdateFile(d.FileName);
            }
        }

        public void UpdateFile(string fileName) {
            if (!System.IO.File.Exists(fileName)) {
                return;
            }

            this.FileName = fileName;
            this.Text = "Zoom Studio - [" + fileName + "]";
            this.tbSource.Text = System.IO.File.ReadAllText(fileName);
        }

        private void viewTheSelectedFunctionInformationToolStripMenuItem_Click(object sender, EventArgs e) {

        }

        private bool ShowToolbar {
            get {
                bool show = Convert.ToBoolean(
                    ConfigurationManager.AppSettings["toolbar"]);
                return show;
            }
            set {
                ConfigurationManager.AppSettings["toolbar"] = value.ToString();
                statusStrip1.Visible = value;
            }
        }

        private bool ShowStatusBar {
            get {
                bool show = Convert.ToBoolean(
                    ConfigurationManager.AppSettings["statusbar"]);
                return show;
            }
            set {
                ConfigurationManager.AppSettings["statusbar"] = value.ToString();
                statusStrip1.Visible = value;
            }
        }

        private void statusBarToolStripMenuItem_Click(object sender, EventArgs e) {
            ShowStatusBar = statusBarToolStripMenuItem.Checked;
        }

        private void toolbarToolStripMenuItem_Click(object sender, EventArgs e) {
            ShowToolbar = toolbarToolStripMenuItem.Checked;
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e) {
            OptionsView view = new OptionsView();

            view.ShowDialog();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e) {
            UpdateFile(string.Empty);
        }

        private void cmdSave_Click(object sender, EventArgs e) {
            SaveFile(sender, e);
        }

        private void cmdNew_Click(object sender, EventArgs e) {
            UpdateFile(string.Empty);
        }

        private void cmdOpen_Click(object sender, EventArgs e) {
            OpenFile();            
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {

        }
    }
}
